<?php

class XMLDataParser extends DataParser {

    public function parseData($data) {
        $xml = simplexml_load_string($data);
        $json = json_encode($xml);
        $array = json_decode($json, TRUE);

        $keyword = $this->getOption('keywords');
        $keywords = explode(",", $keyword);
        $direction = $this->getOption('direction');
        $arrs =$array['row'];
        foreach ($arrs as $array) {
            if (isset($keyword) && isset($array['ORG_NAME']) && isset($direction) && isset($array['AREA_CODE'])) {
                if ($direction == 0) {
                    foreach ($keywords as $key) {
                        if (!strpos($array['ORG_NAME'], $key)) {
                            $str[] = $array;
                        }
                    }
                } else if ($direction == 1) {
                    foreach ($keywords as $key) {
                        if (strpos($array['ORG_NAME'], $key)) {
                            $str[] = $array;
                        }
                    }
                }
            } else {
                $str[] = $array;
            }
        }
        $this->setTotalItems(count($str));
        return $str;
    }
}
